package com.southminority.ethnic.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.southminority.ethnic.pojo.CourseMaterial;
import com.southminority.ethnic.mapper.course.CourseMaterialMapper;
import com.southminority.ethnic.service.course.CourseMaterialService;
import com.southminority.ethnic.controller.coures.vo.CourseMaterialVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author ASHTR
* @description 针对表【course_material(课程资料表)】的数据库操作Service实现
* @createDate 2025-01-20 10:00:00
*/
@Slf4j
@Service
public class CourseMaterialServiceImpl extends ServiceImpl<CourseMaterialMapper, CourseMaterial> implements CourseMaterialService {

    @Override
    public List<CourseMaterialVo> getMaterialsByCourseId(Long courseId) {
        LambdaQueryWrapper<CourseMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseMaterial::getCourseId, courseId)
                .orderByDesc(CourseMaterial::getCreateTime);
        
        List<CourseMaterial> materials = this.list(wrapper);
        
        // 转换为VO
        return materials.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    @Override
    public synchronized Boolean increaseDownloadCount(Long materialId) {
        try {
            CourseMaterial material = this.getById(materialId);
            if (material != null) {
                // 增加下载次数
                material.setDownloadCount(material.getDownloadCount() + 1);
                boolean updated = this.updateById(material);
                if (updated) {
                    log.info("资料下载次数增加成功，资料ID: {}, 新下载次数: {}", materialId, material.getDownloadCount());
                }
                return updated;
            } else {
                log.warn("资料不存在，无法增加下载次数，资料ID: {}", materialId);
                return false;
            }
        } catch (Exception e) {
            log.error("增加资料下载次数失败，资料ID: {}", materialId, e);
            return false;
        }
    }

    /**
     * 将实体转换为VO
     */
    private CourseMaterialVo convertToVo(CourseMaterial material) {
        if (material == null) {
            return null;
        }
        CourseMaterialVo vo = new CourseMaterialVo();
        BeanUtils.copyProperties(material, vo);
        return vo;
    }

    @Override
    public Page<CourseMaterial> getMaterialPageWithKeyword(Page<CourseMaterial> page, String keyword) {
        LambdaQueryWrapper<CourseMaterial> wrapper = new LambdaQueryWrapper<>();
        
        // 如果有关键词，进行模糊查询
        if (StringUtils.hasText(keyword)) {
            wrapper.like(CourseMaterial::getMaterialName, keyword);
        }
        
        // 按创建时间倒序排列
        wrapper.orderByDesc(CourseMaterial::getCreateTime);
        
        return this.page(page, wrapper);
    }
} 